package cn.js.ccit.controller;

import cn.js.ccit.pojo.Record;
import cn.js.ccit.service.IRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private IRecordService recordService;

    @PostMapping("/addRecord")
    public String  addRecord(Record record){
        System.out.println(record);
        System.out.println(record.getBirthday().getYear());
        recordService.insertRecord(record);
        return "recordSuccess";
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile , HttpSession session,String empNo) throws IllegalStateException, IOException {
        System.out.println("empNo="+empNo);
        //获取上传的文件名作为服务器的文件名
        String fileName=uploadFile.getOriginalFilename();

        //获取上传的文件的路径
        String filePath=session.getServletContext().getRealPath("/recordImages");
        System.out.println("filePath="+filePath);

        Record record=new Record();
        record.setEmpNo(empNo);
        record.setPicture(fileName);

        recordService.updateRecordByEmpNo(record);

        File file=new File(filePath,fileName);

        //上传文件
        uploadFile.transferTo(file);

        //保存文件信息到会话中
        session.setAttribute("fileInfo", fileName );
        session.setAttribute("empNo",empNo);

        return "file/showupload";
    }

    @RequestMapping("/uploadForm")
    public String fileUploadForm() {
        return "file/userPictureUpload";
    }







}
